<template>
  <div>
    <a-button class="mb-2 float-right" :disabled="selectedRowKeys.length === 0" type="primary" :loading="btnLoading" @click="onBatchSignStop()">
      批量签停
    </a-button>
    <BaseTable
      fill
      row-key="id"
      :scroll="{ x: 2000 }"
      column-code="001135"
      :loading="loading"
      :data-source="tableData"
      :row-selection="{
        selectedRowKeys,
        getCheckboxProps: (record) => ({
          disabled: record.signStopTime
        }),
        onChange: onSelectChange
      }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'advStatus'">
          <a-tag v-if="record.advStatus === 3" color="red">已停止</a-tag>
        </template>
        <template v-if="column.dataIndex === 'advExecTypeName'">
          {{ record.advExecTypeName }}
        </template>
        <template v-if="column.dataIndex === 'children'">
          <template v-if="record.advOrderType === 0">
            <DoctorAdviceOrder
              :row-data="record"
              :params-key="{
                mc: 'adv.mc',
                gg: 'adv.gg',
                sl: 'clinicalDose',
                dw: 'clinicalDoseUnit',
                yf: 'deliverRoute.fyff',
                pl: 'fqy.plid',
                drugPathName: 'drugPathName'
              }"
            />
          </template>
          <template v-else>
            <a-tooltip>
              <template #title>{{ record?.advName }}</template>
              {{ record?.advName }}
            </a-tooltip>
          </template>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <BaseButton v-if="!record.signStopTime" confirm-text="确定签停吗？" type="link" size="small" @click="onSignStop(record)"> 签停 </BaseButton>
          <BaseButton v-else confirm-text="" type="link" size="small" @click="onOpenSignStop(record)"> 调整签停 </BaseButton>
        </template>
      </template>
    </BaseTable>
    <ChangeSignStop v-model:open="changeSignStopOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import useUserStore from '@/store/user'
import useStore from '../store'
import ChangeSignStop from './ChangeSignStop.vue'

const { empId } = useUserStore()

const store = useStore()
const btnLoading = ref(false)
const selectedRowKeys = ref([])
const changeSignStopOpen = ref(false)
const rowData = ref({})

const { tableData, getData, loading } = useTable({
  url: '/inp/clinicalOrder/getClinicalOrderList',
  pagination: false,
  formatParams: () => {
    return {
      ...store.searchState,
      inpAdvice: undefined,
      queryQt: true
    }
  },
  onSuccess: () => {
    store.loading = false
    selectedRowKeys.value = []
  }
})
const onSelectChange = (keys) => {
  selectedRowKeys.value = keys
}
const onSignStop = async (record) => {
  const res = await post('/inp/clinicalOrder/signStopAdviceInfo', {
    id: record.id,
    qtrEmpId: empId
  })
  btnLoading.value = false
  res.code === 0 ? message.success(res.msg) : message.error(res.msg || '操作失败')
  getData()
  store.fetchAllOrderCountData()
}
const onOpenSignStop = (record) => {
  changeSignStopOpen.value = true
  rowData.value = record
}
const onBatchSignStop = async () => {
  btnLoading.value = true
  const res = await post('/inp/clinicalOrder/signStopAdviceInfo', {
    ids: selectedRowKeys.value,
    qtrEmpId: empId
  })
  btnLoading.value = false
  res.code === 0 ? message.success(res.msg) : message.error(res.msg || '操作失败')
  getData()
  store.fetchAllOrderCountData()
}
defineExpose({
  getData
})
</script>
